#Author：雨霁夜茶
import Load_Okex,Load_Bittrex,Load_ZB,Load_Binance,Load_Bitfinex,Load_Bcex
import time,threading


threads = []
#['Okex', 'Bittrex','Binance', 'Bitfinex','Bcex','ZB', ]
all_datas={"Okex":{},'Bittrex':{},'Binance':{},'Bitfinex':{},'ZB':{},'Bcex':{}}
all_keys_list=['btc','eth','etc']

lock = threading.RLock()

isOkexOut=False
isBittrexOut=False
isBinanceOut=False
isBitfinexOut=False
isBcexOut=False
isZBOut=False

# LoadAllData.change_show_data(isOkexOut, isBittrexOut, isBinanceOut, isBitfinexOut, isBcexOut, isZBOut)
def change_show_data(*args):
    global isOkexOut,isBittrexOut,isBinanceOut,isBitfinexOut,isBcexOut,isZBOut
    isOkexOut=args[0]
    isBittrexOut=args[1]
    isBinanceOut=args[2]
    isBitfinexOut=args[3]
    isBcexOut = args[4]
    isZBOut = args[5]


def det_all_datas_key():
    if isOkexOut:
        all_datas['Okex'].clear()
    if isBittrexOut:
        all_datas['Bittrex'].clear()
    if isBinanceOut:
        all_datas['Binance'].clear()
    if isBitfinexOut:
        all_datas['Bitfinex'].clear()
    if isBcexOut:
        all_datas['Bcex'].clear()
    if isZBOut:
        all_datas['ZB'].clear()

def get_all_datas_func():
    return all_datas
def get_all_keys_list_func():
    return all_keys_list


def load_okex_data(arg):
    isAddKey = False
    while True:
        try:
            lock.acquire()
            data=Load_Okex.load_okex()
            all_datas['Okex']=data[0]
            if isAddKey is False:
                for key in data[1]:
                    all_keys_list.append(key)
                isAddKey = True
            lock.release()
            print('load_okex_data')
        except:
            print('Error---load_okex_data')
        time.sleep(5)

def load_bittrex_data(arg):
    isAddKey=False
    while True:
        try:
            lock.acquire()
            data = Load_Bittrex.load_bittrex_b_USA()
            all_datas['Bittrex'] = data[0]
            if isAddKey is False :
                for key in data[1]:
                    all_keys_list.append(key)
                isAddKey = True
            lock.release()
            print('load_bittrex_data')
        except:
            print('Error---load_bittrex_data')
        time.sleep(5)

def load_zb_data(arg):
    isAddKey=False
    while True:
        try:
            lock.acquire()
            data = Load_ZB.load_zb()
            all_datas['ZB'] = data[0]
            if isAddKey is False :
                for key in data[1]:
                    all_keys_list.append(key)
                isAddKey = True
            lock.release()
            print('load_zb_data')

        except:
            print('Error---load_zb_data')
        time.sleep(5)

def load_binance_data(arg):
    isAddKey=False
    while True:
        try:
            lock.acquire()
            data = Load_Binance.load_data()
            all_datas['Binance'] = data[0]
            if isAddKey is False :
                for key in data[1]:
                    all_keys_list.append(key)
                isAddKey = True
            lock.release()
            print('load_binance_data')

        except:
            print('Error---load_binance_data')
        time.sleep(5)
def load_bitfinex_data(arg):
    isAddKey=False
    while True:
        try:
            lock.acquire()
            data = Load_Bitfinex.load_data()
            all_datas['Bitfinex'] = data[0]
            if isAddKey is False :
                for key in data[1]:
                    all_keys_list.append(key)
                isAddKey = True
            lock.release()
            print('load_bitfinex_data')

        except:
            print('Error---load_bitfinex_data')
        time.sleep(5)

def load_bcex_data(arg):
    isAddKey=False
    while True:
        try:
            lock.acquire()
            data = Load_Bcex.load_data()
            all_datas['Bcex'] = data[0]
            if isAddKey is False :
                for key in data[1]:
                    all_keys_list.append(key)
                isAddKey = True
            lock.release()
            print('load_Bcex_data')

        except:
            print('Error---load_Bcex_data')
        time.sleep(5)

# def get_bittrex_data():
#     while True:
#         try:
#             lock.acquire()
#             data = Load_Bittrex.load_bittrex_b_USA()
#             all_datas['Bittrex'] = Load_Bittrex.data_dict_bittrex
#             for key in Load_Bittrex.keys:
#                 all_keys_list.append(key)
#             lock.release()
#             print('Get load_bittrex_data:',len(all_datas['Bittrex']))
#             time.sleep(5)
#         except:
#             print('Error---load_bittrex_data')


def load_all_data():
    pass

itemdata_1=None;
itemdata_2=None;
def calculate_data(arg):
    while True:
        time.sleep(1)
        lock.acquire()
        try:



            all_keys_set=set(all_keys_list)
            # keylist=list(all_keys_set)
            det_all_datas_key()

            for k,value in all_datas.items():
                # if k=='Okex':
                #     continue
                # else:
                for key in all_keys_set:
                    for k2, value2 in all_datas.items():
                        itemdata___0=all_datas[k2].get(key)
                        if itemdata___0 is not None:
                            itemdata___0['precent']=0
                    for k2, value2 in all_datas.items():
                        itemdata_1=all_datas[k2].get(key)
                        if itemdata_1 is not None:
                            break
                    for k2, value2 in all_datas.items():
                        itemdata_2=all_datas[k2].get(key)
                        if itemdata_1 !=itemdata_2 and itemdata_2 is not None:
                            if itemdata_1 is not None and itemdata_2 is not None:
                                precent = (itemdata_2['usdt'] - itemdata_1['usdt']) / itemdata_1['usdt']
                                # itemdata_2['precent'] = round(precent, 5)
                                itemdata_2['precent'] = precent

                        # print('key:',key, '  ' ,itemdata_2['usdt'],'   ',itemdata_1['usdt'],'  ',
                        #        (itemdata_2['usdt'] - itemdata_1['usdt']),'  precent:',precent)
            lock.release()
            print('计算数据')
        except:
            print('Calculate Error')
        # return all_datas

def add_threads():
    t1 = threading.Thread(target=load_okex_data,args=(u'爱情买卖',))
    threads.append(t1)
    t2 = threading.Thread(target=load_bittrex_data,args=(u'爱情买卖',))
    threads.append(t2)

    t3 = threading.Thread(target=calculate_data, args=(u'爱情买卖',))
    threads.append(t3)

    t4 = threading.Thread(target=load_zb_data, args=(u'爱情买卖',))
    threads.append(t4)
    t5 = threading.Thread(target=load_binance_data, args=(u'爱情买卖',))
    threads.append(t5)

    t6 = threading.Thread(target=load_bitfinex_data, args=(u'爱情买卖',))
    threads.append(t6)

    t7 = threading.Thread(target=load_bcex_data, args=(u'爱情买卖',))
    threads.append(t7)

def start_load_data():
    add_threads()
    for t in threads:
        t.start()

# start_load_data()



